﻿
function extraInit()
{
    initTree(false, false, false);
}

function equalizeNameCells(table)
{
}

function editWorktime(button)
{
    //temporarily disable
    return;
    //
    var bar = $(button.parentNode);
    var agentId = bar.find("input[name=agentId]").val();
    var date = $(button).find("input[name=date]").val();
    var argsVariable = new Object();
    var answer = window.showModalDialog("/Worktime/EditWorktime.aspx?agentId=" + agentId + "&date=" + date,
        argsVariable, 
        "dialogWidth:500px; dialogHeight:500px; dialogLeft:250px; dialogTop:100px; center:yes;resizable:no;status:no; statusbar:no; toolbar:no; menubar:no; addressbar:no; titlebar:no;");
    
    refreshCell(agentId, date);
}

function addWorktime(button)
{
    var tr = $(button.parentNode.parentNode);
    var divClone = tr.find("div[id=divWorktimeClone]");
    var td = divClone.parent();
    var clone2 = divClone.clone();
    clone2.removeAttr("id");
    clone2.removeAttr("style");
    clone2.find("input[name=hdfWorktimeId2]").attr("name", "hdfWorktimeId");
    clone2.find("select[name=cbxStartHour2]").attr("name", "cbxStartHour");
    clone2.find("select[name=cbxStartMinute2]").attr("name", "cbxStartMinute");
    clone2.find("select[name=cbxEndHour2]").attr("name", "cbxEndHour");
    clone2.find("select[name=cbxEndMinute2]").attr("name", "cbxEndMinute");
    td.append(clone2);
}

function removeWorktime(button)
{
    var div = $(button.parentNode);
    var hdfId = div.find("input[name=hdfWorktimeId]");
    if (hdfId.val() != "")
    {
        var td = $(button.parentNode.parentNode);
        var divRemoved = td.find("div[id=divRemovedWorktimes]");
        divRemoved.append(hdfId);
        hdfId.attr("name", "hdfRemovedWorktimeId");
    }
    div.remove();
}

function selectMonth()
{
    var month = parseInt($("input[name=tbxMonth]").val());
    var year = parseInt($("input[name=tbxYear]").val());
    if (isNaN(month) || isNaN(year))
    {
        var date = new Date();
        month = date.getMonth();
        year = date.getFullYear();
    }
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    viewWorktimeSheet(jobTitleId, year, month, 1);
}

function previousMonth()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    month--;
    if (month < 1)
    {
        year--;
        month = 12;
    }
    viewWorktimeSheet(jobTitleId, year, month, 1);
}

function nextMonth()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    month++;
    if (month > 12)
    {
        year++;
        month = 1;
    }
    viewWorktimeSheet(jobTitleId, year, month, 1);
}

function previousYear()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    year--;
    viewWorktimeSheet(jobTitleId, year, month, 1);
}

function nextYear()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    year++;
    viewWorktimeSheet(jobTitleId, year, month, 1);
}

function selectTreeNode(jobTitleId)
{
    //$("input[name=jobTitleId]").val(jobTitleId);
    //$("#btnViewMonth").click();
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    viewWorktimeSheet(jobTitleId, year, month, 1);
}

function selectPage(index)
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    viewWorktimeSheet(jobTitleId, year, month, index);
}

function viewWorktimeSheet(jobTitleId, year, month, pageIndex)
{
    $("input[name=tbxMonth]").val(month);
    $("input[name=tbxYear]").val(year);
    $("#hdfMonth").val(month);
    $("#hdfYear").val(year);
    
    var area = mask();
    $.ajax({
	    async : false,
	    type: 'POST',
	    url: "/AjaxWorktime.aspx",
	    data : { 
		    "action" : "get_worktime_sheet", 
		    "jobTitleId" : jobTitleId,
		    "year" : year,
		    "month" : month,
		    "pageIndex" : pageIndex
	    }, 
	    success : function (r) {
	        if (r.status)
			{
			    var table = $("#worktimeTable");
			    var rows = table.find(".row");
			    rows.eq(0).find("div").eq(0).text(month + "/" + year);
			    for (var i=rows.length-1; i>0; i--)
			    {
			        rows.eq(i).remove();
			    }
			    for (var i=0; i<r.list.length; i++)
			    {
			        var htmlRow = "<div class='row'>";
			        htmlRow += "<div class='name-cell a2'>"+r.list[i].fullName+"</div>";
			        htmlRow += "<input name='agentId' value='"+r.list[i].agentId+"' type='hidden'>";
			        var k;
			        for (k=0; k<r.list[i].cell.length; k++)
			        {
			            htmlRow += "<div class='cell tooltip' ";
			            if (r.list[i].cell[k].bonus)
			            {
			                htmlRow += ">";
			            }
			            else
			            {
			                htmlRow += " onclick='editWorktime(this);'>";
			            }
			            htmlRow += r.list[i].cell[k].total;
                        htmlRow += "<input name='date' value='" + r.list[i].cell[k].date + "' type='hidden'>";
                        htmlRow += "<div class='tooltip-content'>";
                        htmlRow += "<img class='callout' src='/image/layout/callout.gif'>";
                        for (var j=0; j<r.list[i].cell[k].activities.length; j++)
                        {
                            //temporarily comment
                            //htmlRow += r.list[i].cell[k].activities[j] + "<br/>";
                        }
                        if (r.list[i].cell[k].description != "")
                        {
                            htmlRow += "<div style='white-space:pre'>" + r.list[i].cell[k].description + "</div><br/>";
                        }
                        if (r.list[i].cell[k].total != "1.0")
                        {
                            htmlRow += r.list[i].cell[k].leaveApplication+"<br/>";
                        }
                        htmlRow += "</div></div>";
                    }
                    while (k < 32)
                    {
                        htmlRow += "<div class='cell tooltip'></div>";
                        k++;
                    }
                    htmlRow += "<div class='cell-total'>"+r.list[i].totalRow+"</div>";
                    htmlRow += "</div>";
                    
                    table.append(htmlRow);
			    }
			    
			    var divPaging = $("#divPaging");
			    var paging = "";
			    for (var i=1; i<=r.totalPage; i++)
			    {
			        if (r.pageIndex == i)
			        {
			            paging += "<span>"+i+"</span>";
			        }
			        else
			        {
			            paging += "<a href='javascript:void(0);' onclick='selectPage("+i+");'>"+i+"</a>";
			        }
			    }
			    divPaging.html(paging);
			}
	    }
    })
    .always(function() {
	    unmask(area);
	});
}

function refreshCell(agentId, date)
{
    var table = $("#worktimeTable");
    var rows = table.find(".row");
    for (var i=1; i<rows.length; i++)
    {
        var hdfAgentId = rows.eq(i).find("input[name=agentId]");
        if (hdfAgentId.val() == agentId)
        {
            var cells = rows.eq(i).find(".cell");
            for (var j=0; j<cells.length; j++)
            {
                var hdfDate = cells.eq(j).find("input[name=date]");
                if (hdfDate.val() == date)
                {
                    refreshWorktime(cells.eq(j), agentId, date);
                    break;
                }
            }
            break;
        }
    }
}

function refreshWorktime(cell, agentId, date)
{
    var area = mask();
    $.ajax({
	    async : false,
	    type: 'POST',
	    url: "/AjaxWorktime.aspx",
	    data : { 
		    "action" : "get_worktime", 
		    "agentId" : agentId,
		    "date" : date
	    }, 
	    success : function (r) {
	        if (r.status)
			{
                var htmlCell = r.worktime.total;
                htmlCell += "<input name='date' value='" + date + "' type='hidden'>";
                htmlCell += "<div class='tooltip-content'>";
                htmlCell += "<img class='callout' src='/image/layout/callout.gif'>";
                for (var j=0; j<r.worktime.activities.length; j++)
                {
                    htmlCell += r.worktime.activities[j] + "<br/>";
                }
                if (r.worktime.description != "")
                {
                    htmlCell += "<div style='white-space:pre'>" + r.worktime.description + "</div><br/>";
                }
                if (r.worktime.total != "1.0")
                {
                    if (r.worktime.isApproved)
                    {
                        htmlCell += r.worktime.leaveApplication + "<br/>";
                    }
                    else
                    {
                        htmlCell += "<span style='color:red'>"+r.worktime.leaveApplication+"</span><br/>";
                    }
                }
                cell.html(htmlCell);
                
                var lastCellHtml = r.bonus.total;
                lastCellHtml += "<div class='tooltip-content'>";
                lastCellHtml += "<img class='callout' src='/image/layout/callout.gif'/>";
                lastCellHtml += "<div style='white-space:pre'>"+r.bonus.description+"</div><br>";
                lastCellHtml += "</div>";
                var row = cell.parent();
                var cells = row.find(".cell");
                cells.eq(cells.length-1).html(lastCellHtml);
            }
        }
    })
    .always(function() {
	    unmask(area);
	});
}


function setDefaultWorktime()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var li = $("a.jstree-clicked").closest("li");
    if (li.length  == 0)
    {
        return;
    }
    var jobTitleId = li.attr("id");
    
    var updateSuccess = false;
    var area = mask();
    $.ajax({
	    async : false,
	    type: 'POST',
	    url: "/AjaxWorktime.aspx",
	    data : { 
		    "action" : "set_default_worktime", 
		    "jobTitleId" : jobTitleId,
		    "year" : year,
		    "month" : month
	    }, 
	    success : function (r) {
	        if (r.status)
			{
                updateSuccess = true;
                viewWorktimeSheet(jobTitleId, year, month, 1);
            }
            else
            {
                if (r.error != "")
                {
                    alert(r.error);
                }
            }
        }
    })
    .always(function() {
        //if (!updateSuccess)
        {
	        unmask(area);
	    }
	});
}

function exportExcel()
{
    var li = $("a.jstree-clicked").closest("li");
    if (li.length  == 0)
    {
        return;
    }
    $("#hdfJobTitleId").val(li.attr("id"));
}



